//
// DotNetNuke - http://www.dotnetnuke.com
// Copyright (c) 2002-2010
// by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.IO;
using DotNetNuke.Entities.Host;
using DotNetNuke.Services.Localization;
namespace DotNetNuke.Modules.SearchInput
{
	partial class SearchInput : Entities.Modules.PortalModuleBase
	{
		private void ShowHideImages()
		{
			string ShowGoImage = Convert.ToString(Settings["ShowGoImage"]);
			string ShowSearchImage = Convert.ToString(Settings["ShowSearchImage"]);
			bool bShowGoImage = false;
			bool bShowSearchImage = false;
			if (ShowGoImage != null) {
				bShowGoImage = Convert.ToBoolean(ShowGoImage);
			}
			if (ShowSearchImage != null) {
				bShowSearchImage = Convert.ToBoolean(ShowSearchImage);
			}
			imgSearch.Visible = bShowSearchImage;
			plSearch.Visible = !bShowSearchImage;
			imgGo.Visible = bShowGoImage;
			cmdGo.Visible = !bShowGoImage;
		}
		private void SearchExecute()
		{
			int ResultsTabid;
			if (Settings["SearchResultsModule"] != null) {
				ResultsTabid = int.Parse(Convert.ToString(Settings["SearchResultsModule"]));
			} else {
				Entities.Modules.ModuleController objModules = new Entities.Modules.ModuleController();
				DotNetNuke.Entities.Modules.ModuleInfo SearchModule = objModules.GetModuleByDefinition(PortalSettings.PortalId, "Search Results");
				if (SearchModule == null) {
					DotNetNuke.UI.Skins.Skin.AddModuleMessage(this, Localization.GetString("NoSearchModule", LocalResourceFile), DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.YellowWarning);
					return;
				} else {
					ResultsTabid = SearchModule.TabID;
				}
			}
			if (Host.UseFriendlyUrls) {
				Response.Redirect(Common.Globals.NavigateURL(ResultsTabid) + "?Search=" + Server.UrlEncode(txtSearch.Text));
			} else {
				Response.Redirect(Common.Globals.NavigateURL(ResultsTabid) + "&Search=" + Server.UrlEncode(txtSearch.Text));
			}
		}
		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);

			imgGo.Click += new System.Web.UI.ImageClickEventHandler(imgGo_Click);
			cmdGo.Click += new EventHandler(cmdGo_Click);

			string GoUrl = Services.Localization.Localization.GetString("imgGo.ImageUrl", this.LocalResourceFile);
			string SearchUrl = Services.Localization.Localization.GetString("imgSearch.ImageUrl", this.LocalResourceFile);
			if (GoUrl.StartsWith("~")) {
				imgGo.ImageUrl = GoUrl;
			} else {
				imgGo.ImageUrl = Path.Combine(PortalSettings.HomeDirectory, GoUrl);
			}
			if (SearchUrl.StartsWith("~")) {
				imgSearch.ImageUrl = SearchUrl;
			} else {
				imgSearch.ImageUrl = Path.Combine(PortalSettings.HomeDirectory, SearchUrl);
			}
			ShowHideImages();
			cmdGo.Text = Services.Localization.Localization.GetString("cmdGo.Text", this.LocalResourceFile);

		}
		private void imgGo_Click(System.Object sender, System.Web.UI.ImageClickEventArgs e)
		{
			SearchExecute();
		}
		private void cmdGo_Click(object sender, System.EventArgs e)
		{
			SearchExecute();
		}
	}
}
